<%-- 
    Document   : mailingList
    Created on : Dec 6, 2011, 10:24:19 PM
    Author     : Michael
--%>

<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.File"%>
<%@page import="javax.swing.JFileChooser"%>
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
  <% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>      
<% 
            Connection conn = null;
            try
            {
                conn = new DBClasses.GetConnection().getConnection();
                ResultSet rs = conn.createStatement().executeQuery(("SELECT Email FROM customer ORDER BY Email"));
                
                JFileChooser jfc = new JFileChooser();
                jfc.setSelectedFile(new File("mailinglist.csv"));
                int option = jfc.showSaveDialog(null);
                if(option==JFileChooser.APPROVE_OPTION)
                {
                    File f = jfc.getSelectedFile();
                    FileWriter fw = new FileWriter(f);
                    PrintWriter pw = new PrintWriter(fw);
                    while(rs.next())
                    {
                        pw.print(rs.getString("Email") + ",");
                    }
                    pw.flush();
                    pw.close();
                    
                    JOptionPane.showMessageDialog(null, "Mailing List Saved");
                    
                }
                
                
            }
            catch(Exception e)
            {
                e.printStackTrace();     
                response.sendRedirect("gohome.jsp");
            }
            finally
            {
                try { conn.close(); response.sendRedirect("gohome.jsp"); }
                catch(Exception e)
                {
                    e.printStackTrace();   
                    response.sendRedirect("gohome.jsp");                        
                }
            }
        
        %>